package com.yizhubao.app.activitys.user;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yizhubao.app.R;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.customviews.BottomDialog;
import com.yizhubao.app.utils.Constants;
import com.yizhubao.app.utils.Utils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.utils.DateUtil;
import h.framework.pacy.utils.ImageUtil;
import okhttp3.internal.Util;

/**
 * 创建人: PACY
 * 创建时间: 6/4/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 生命健康检测
 */
public class HealthyActivity extends BaseActivityToolBar {


    @BindView(R.id.time_tv)
    TextView time_tv;
    @BindView(R.id.title_tv)
    TextView title_tv;
    private CountDownTimer countDownTimer ;


    @BindView(R.id.lab_tv)
    TextView lab_tv;
    @BindView(R.id.img_iv)
    ImageView img_iv;
    @BindView(R.id.progress)
    ProgressBar progress;

    private MediaPlayer mediaPlayer = new MediaPlayer();
    @Override
    protected int setContentLayout() {
        return R.layout.activity_healthy;
    }

    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
    }

    @Override
    protected void initData() {
        initMediaPlayer();
        start();
    }

    /**
     * 判断是否实名
     */
    private void setUI(){
        progress.setVisibility(View.GONE);
        img_iv.setVisibility(View.VISIBLE);
        //双认证
        if(Utils.isPay()&&Utils.isIdCard()){
            String lab="数据获取自"+DateUtil.getNowDate(DateUtil.DatePattern.ONLY_MINUTE_C)+"\n平台正在实时保障关注您的健康状态";
            lab_tv.setText(lab);
            String temp=(DateUtil.getNowDay()+ Integer.parseInt(Utils.getUserId()))%10+".gif";
            ImageUtil.loadGIF(Constants.HEALTHY_LIST+temp,img_iv);
            lab_tv.setOnClickListener(v -> {
                    BottomDialog.DialogBuilder(HealthyActivity.this).title("已生效·保障中")
                            .message("平台为您提供的服务已经生效，背后上千名工作人员，正在实时保障关注您的健康状态。若发现您遭遇意外，将第一时间和联系人确认，并移交相关重要内容。也会协助联系人，为您处理后事 .....")
                            .completeText("知道了").completeCallBack(() -> {
                    }).show();
            });
        }else{
            ImageUtil.loadGIF(Constants.HEALTHY_LIST+"11.gif",img_iv);
            lab_tv.setText("上面是示例数据。只有通过实名认证，并且\n成功创建内容后，才会展示您本人的真实数据");
        }
    }

    /**
     * 初始化播放器
     */
    private void initMediaPlayer() {
        try {
            mediaPlayer.setDataSource("https://resource.weimingfang.com/company/yinpin1.mp3");
            mediaPlayer.prepare();
            if (!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 开始
     */
    public void start() {
        if(countDownTimer==null){
            countDownTimer = new CountDownTimer(10*1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    String str = "检测中 ... "+((millisUntilFinished / 1000 )+1);
                    time_tv.setText(str);
                }

                @Override
                public void onFinish() {
                    setUI();
                    try {
                        if( mediaPlayer!=null){
                            mediaPlayer.reset();
                            mediaPlayer.setDataSource("https://resource.weimingfang.com/company/yinpin2.mp3");
                            mediaPlayer.prepare();
                            if (!mediaPlayer.isPlaying()) {
                                mediaPlayer.start();
                            }
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    title_tv.setText("检测成功");
                    time_tv.setText("目前一切正常！");
                }
            };
        }

        countDownTimer.start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
        }

    }
}